﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;

namespace WindowsWeChat.Control
{
    /// <summary>
    /// VoiceControl.xaml 的交互逻辑
    /// </summary>
    public partial class VoiceControl : UserControl
    {
        public VoiceControl()
        {
            InitializeComponent();
        }
        private static readonly DependencyProperty FillColorFristProperty = DependencyProperty.Register("FillFristColor", typeof(SolidColorBrush), typeof(UserControl));
        /// <summary>
        /// 填充第一个颜色
        /// </summary>
        public SolidColorBrush FillFristColor
        {
            get
            {
                return (SolidColorBrush)GetValue(FillColorFristProperty);
            }
            set
            {
                SetValue(FillColorFristProperty, value);
            }
        }
        private static readonly DependencyProperty FillColorTwoProperty = DependencyProperty.Register("FillTwoColor", typeof(SolidColorBrush), typeof(UserControl));
        /// <summary>
        /// 第二个颜色
        /// </summary>
        public SolidColorBrush FillTwoColor
        {
            get
            {
                return (SolidColorBrush)GetValue(FillColorTwoProperty);
            }
            set
            {
                SetValue(FillColorTwoProperty, value);
            }
        }
        private static readonly DependencyProperty FillColorThreeProperty = DependencyProperty.Register("FillThreeColor", typeof(SolidColorBrush), typeof(UserControl));
        /// <summary>
        /// 第三个颜色填充项
        /// </summary>
        public SolidColorBrush FillThreeColor
        {
            get
            {
                return (SolidColorBrush)GetValue(FillColorThreeProperty);
            }
            set
            {
                SetValue(FillColorThreeProperty, value);
            }
        }
        private static readonly DependencyProperty FilePathProperty = DependencyProperty.Register("FilePath", typeof(string), typeof(VoiceControl));
        public string FilePath
        {
            get
            {
                return (string)GetValue(FilePathProperty);
            }
            set
            {
                SetValue(FilePathProperty, value);
            }
        }
        bool Flag = true;
        MediaPlayer player = new MediaPlayer();
        private void StackPanel_PreviewMouseLeftButtonDown(object sender, MouseButtonEventArgs e)
        {
            Flag = true;
            new Task(() =>
            {
                this.Dispatcher.Invoke(new Action(() =>
                {
                    //ME.Source = new Uri(FilePath);
                    //ME.LoadedBehavior = MediaState.Manual;
                    //ME.Play();
                    player.Open(new Uri(FilePath));
                    player.Play();
                }));
            }).Start();
            int index = 0;
            new Task(() =>
            {
                while (Flag)
                {
                    this.Dispatcher.Invoke(new Action(() =>
                    {
                        switch (index)
                        {
                            case 0:
                                FillFristColor = Brushes.Red;
                                break;
                            case 1:
                                FillTwoColor = Brushes.Green;
                                break;
                            case 2:
                                FillThreeColor = Brushes.Purple;
                                break;
                        }
                    }));
                    index++;
                    Thread.Sleep(500);
                    if (index > 2)
                    {
                        this.Dispatcher.Invoke(new Action(() =>
                        {
                            FillThreeColor = Brushes.Transparent;
                            FillTwoColor = Brushes.Transparent;
                        }));
                        index = 0;
                    }
                }
            }).Start();
        }
    }

}
